<?php
class ScheduleApp extends MemberbaseApp
{
	var $_schedule_mod;
	var $_member_mod;
	
	function __construct(){
		parent::__construct();
		$this->ScheduleApp();
	}
	
	function ScheduleApp(){
		$this->_schedule_mod=&m('schedule');
		$this->_member_mod=&m('member');
	}
	
	
	function index(){
		$user=$this->visitor->get();
		$conditions="1=1";
		if (in_array($user['role'], array(USER_MERCHANDISER))){
			$conditions.=" and freight_id=".$user['user_id']." and role=".USER_CLIENT;
		}elseif (in_array($user['role'], array(USER_MANAGER))){
			$conditions.=" and role=".USER_CLIENT;
		}elseif ($user['role']==USER_MERCHANDISER_ADMIN){
			$mod_cm=&m("company_manager");
			$mod_brand=&m("brand");
			$brand_ids=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
			$brands=array_keys($brand_ids);
			$user_mod =& m('member');
			$buyer_list=$user_mod->find(array(
					'conditions'=>'role='.USER_CLIENT.' and activation=1'
			));
			$flag=0;
			if (!empty($buyer_list)){
				foreach ($buyer_list as $j=>$row){
					$brand_arr_id=explode(',', $row['brand_arr_id']);
					$brand_arr=array_merge($brand_arr_id,$brands);
					$brand_arr_ids=array_count_values($brand_arr);
					if (!empty($brand_arr_ids)){
						foreach ($brand_arr_ids as $i=>$r){
							if ($brand_arr_ids[$i]>1){
								$flag=1;
								break;
							}else{
								$flag=0;
							}
						}
					}
					if ($flag){
						$freight_id[]=$row['freight_id'];
					}
				}
			}
			$conditions.=" and role=".USER_CLIENT." and activation=1 and if_show=1 and freight_id".db_create_in(array_unique($freight_id));
		}else{
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$page=$this->_get_page();
		$schedule_list=$this->_member_mod->find(array(
			'conditions'=>$conditions,
			'count'=>true,
			'limit'=>$page['limit'],
			'order'=>'reg_time desc'	
		));
		if (!empty($schedule_list)){
			foreach ($schedule_list as $key=>$val){
				$consumer_id=$val['user_id'];
				$schedule_info=$this->_schedule_mod->find(array(
					'conditions'=>'consumer_id='.$consumer_id,
					'limit'=>5,
					'order'=>'schedule_date desc'	
				));
				$schedule_list[$key]['schedule_info']=$schedule_info;
			}
		}
		$page['item_count'] = $this->_member_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('schedule_list',$schedule_list);
		$this->assign('user',$user);
		$this->display('schedule.index.html');
		$this->_curlocal(Lang::get('consumer_schedule'));
	}
	
	
	function add(){
		$user=$this->visitor->get();
		if (!in_array($user['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			$this->show_warning("no_powers_to_visit");
			return;
		}
		if(!IS_POST){
			if ($user['role']==USER_MERCHANDISER){
				$this->assign('buyer_list', $this->_member_mod->find(array(
						'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT.' and freight_id='.$user['user_id'],
						'order' => 'user_name asc'
				)));
			}elseif ($user['role']==USER_MERCHANDISER_ADMIN){
				$user_arr_id=$this->child_user();
				$this->assign('buyer_list', $this->_member_mod->find(array(
						'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT.' and user_id'.db_create_in($user_arr_id),
						'order' => 'user_name asc'
				)));
			}else{
				$this->assign('buyer_list', $this->_member_mod->find(array(
						'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
						'order' => 'user_name asc'
				)));
			}
			$this->assign('belongs_consumer_schedule',BELONGS_CONSUMER_SCHEDULE);
			$this->assign('default_consumer_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('schedule.form.html');
			$this->_curlocal(Lang::get('consumer_schedule'),'index.php?app=schedule',Lang::get('add_schedule'));
		}else{
			$data=array();
			$title=$_POST['schedule_title'];
			$context=$_POST['schedule_context'];
			$consumer_id=$_POST['buyer_id'];
			if (!$title){
				$this->show_warning("title_required");
				return;
			}
			if (!$context){
				$this->show_warning("context_required");
				return;
			}
			if (!$consumer_id){
				$this->show_warning("请选择客户");
				return;
			}
			$data['schedule_date']=gmtime();
			$data['schedule_title']=$title;
			$data['schedule_context']=$context;
			$data['handle_user_id']=$user['user_id'];
			$data['consumer_id']=$consumer_id;
			$consumer_schedule_id=isset($_POST['consumer_schedule_id'])?$_POST['consumer_schedule_id']:0;
			$schedule_id=$this->_schedule_mod->add($data);
			if ($this->_schedule_mod->has_error()){
				$this->show_warning($this->_schedule_mod->get_error());
				return;
			}
			$file_mod =& m('upload');
			if(!empty($consumer_schedule_id))
			{
				$file_ids=implode(",",$consumer_schedule_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_CONSUMER_SCHEDULE." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
				 
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $schedule_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$schedule_id,
							"session_id"=>session_id()
					));
				}
			}
			$this->show_message("add_schedule_success",Lang::get('consumer_schedule'),'index.php?app=schedule&act=index');
		}
	}
	
	
	function show(){
		$user=$this->visitor->get();
		if (!in_array($user['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$id=isset($_GET['id'])?$_GET['id']:0;
		$file_mod=&m('upload');
		if (!$id){
			$this->show_warning("该进度不存在");
			return;
		}
		$schedule=$this->_schedule_mod->get($id);
		if ($user['role']==USER_MERCHANDISER){
			$this->assign('buyer_list', $this->_member_mod->find(array(
					'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT.' and freight_id='.$user['user_id'],
					'order' => 'user_name asc'
			)));
		}else{
			$this->assign('buyer_list', $this->_member_mod->find(array(
					'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
					'order' => 'user_name asc'
			)));
		}
		$file_schedule=$file_mod->find(array(
			'conditions'=>'file_type = '.BELONGS_CONSUMER_SCHEDULE.' AND key_id = '.$id,
			'order'=>'file_id ASC'
		));
		$this->assign('file_schedule',$file_schedule);
		$this->assign('schedule',$schedule);
		$this->assign('user',$user);
		$this->display('schedule.show.html');
		$this->_curlocal(Lang::get('consumer_schedule'),'index.php?app=schedule','查看进度');
	}
	
	
	function all_schedule(){
		$uid=isset($_GET['uid'])?$_GET['uid']:0;
		$user=$this->visitor->get();
		if (!$uid){
			$this->show_warning("该用户不存在");
			return;
		}
		$page=$this->_get_page();
		$schedule_list=$this->_schedule_mod->find(array(
			'join'=>'has_member',
			'conditions'=>'consumer_id='.$uid,
			'order'=>'schedule_date desc',
			'count'=>true,
			'limit'=>$page['limit']	
		));
		$page['item_count'] = $this->_schedule_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('user',$user);
		$this->assign('schedule_list',$schedule_list);
		$this->display('schedule.all.html');
		$this->_curlocal(Lang::get('consumer_schedule'),'index.php?app=schedule',"用户进度列表");
	}
	
	function drop(){
		$user=$this->visitor->get();
		$id=isset($_GET['id'])?$_GET['id']:0;
		if (!$id){
			$this->show_warning("该进度不存在");
			return;
		}
		$this->_schedule_mod->drop($id);
		if($this->_schedule_mod->has_error()){
			$this->show_warning($this->_schedule_mod->get_error());
			return;
		}
		$this->show_message("删除成功");
	}
	
	
	function edit(){
		$user=$this->visitor->get();
		if (!in_array($user['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			$this->show_warning("no_powers_to_visit");
			return;
		}
		$id=isset($_GET['id'])?$_GET['id']:0;
		$file_mod=&m('upload');
		if (!$id){
			$this->show_warning("该进度不存在");
			return;
		}
		if (!IS_POST){
			$schedule=$this->_schedule_mod->get($id);
			if ($user['role']==USER_MERCHANDISER){
				$this->assign('buyer_list', $this->_member_mod->find(array(
						'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT.' and freight_id='.$user['user_id'],
						'order' => 'user_name asc'
				)));
			}else{
				$this->assign('buyer_list', $this->_member_mod->find(array(
						'conditions' => 'activation=1 and if_show = 1 and role='.USER_CLIENT,
						'order' => 'user_name asc'
				)));
			}
			$file_schedule=$file_mod->find(array(
					'conditions'=>'file_type = '.BELONGS_CONSUMER_SCHEDULE.' AND key_id = '.$id,
					'order'=>'file_id ASC'
			));
			$this->assign('file_schedule',$file_schedule);
			$this->assign('belongs_consumer_schedule',BELONGS_CONSUMER_SCHEDULE);
			$this->assign('schedule',$schedule);
			$this->assign('user',$user);
			$this->display('schedule.form.html');
			$this->_curlocal(Lang::get('consumer_schedule'),'index.php?app=schedule',Lang::get('edit_schedule'));
		}else{
			$data=array();
			$title=$_POST['schedule_title'];
			$context=$_POST['schedule_context'];
			$consumer_id=$_POST['buyer_id'];
			if (!$title){
				$this->show_warning("title_required");
				return;
			}
			if (!$context){
				$this->show_warning("context_required");
				return;
			}
			if (!$consumer_id){
				$this->show_warning("请选择客户");
				return;
			}
			$data['schedule_title']=$title;
			$data['schedule_context']=$context;
			$data['handle_user_id']=$user['user_id'];
			$data['consumer_id']=$consumer_id;
			$schedule_id=$this->_schedule_mod->edit($id,$data);
			if ($this->_schedule_mod->has_error()){
				$this->show_warning($this->_schedule_mod->get_error());
				return;
			}
			$this->show_message("edit_schedule_success");
		}
	}
	
	
	function perf_total(){
		$yea = intval($_GET['year']);
		$year=$yea ? $yea : date('Y');
		$consumer_order=intval($_GET['consumer_order']);
		$years = array();
		$this_year = $year ? $year : date('Y');
		for($i = $this_year - 10; $i <= $this_year+1; $i++) {
			$years[$i] = $i;
		}
		$order_mod=&m('order');
		$this->assign('years', $years);
		$this->assign('this_year', $this_year);
		$user=$this->visitor->get();
		if ($user['role']==USER_MERCHANDISER_ADMIN){
			$mod_cm=&m("company_manager");
			$mod_brand=&m("brand");
			$brand_id=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
			$brands=array_keys($brand_id);
			$user_mod =& m('member');
			$buyer_list=$user_mod->find(array(
					'conditions'=>'role='.USER_CLIENT.' and activation=1'
			));
			$flag=0;
			if (!empty($buyer_list)){
				foreach ($buyer_list as $j=>$row){
					$brand_arr_id=explode(',', $row['brand_arr_id']);
					$brand_arr=array_merge($brand_arr_id,$brands);
					$brand_arr_ids=array_count_values($brand_arr);
					if (!empty($brand_arr_ids)){
						foreach ($brand_arr_ids as $i=>$r){
							if ($brand_arr_ids[$i]>1){
								$flag=1;
								break;
							}else{
								$flag=0;
							}
						}
					}
					if ($flag){
						$freight_id[]=$row['freight_id'];
					}
				}
			}
			$conditions.=" and freight_id".db_create_in(array_unique($freight_id));
			$con.=" and user_id".db_create_in(array_unique($freight_id));
		}
		if ($consumer_order){
			$user_list = $this->_member_mod->find(array(
					'conditions' => 'if_show = 1 and role='.USER_CLIENT.$conditions,
					'order' => 'user_name ASC'
			));
			if (!empty($user_list)){
				foreach ($user_list as $j=>$rows){
					$freight_id=$rows['freight_id'];
					$uinfo=$this->_member_mod->get(array(
						'conditions'=>'user_id='.$freight_id,
						'fields'=>'user_name'	
					));
					$user_list[$j]['merch_name']=$uinfo['user_name'];
				}
			}
			foreach ($user_list as $key => $val) {
				$orders = $order_mod->find(array(
						'conditions' => 'buyer_id = ' . $val['user_id'] . ' AND order_create_date >= ' . strtotime($year . '-1-1') . ' AND order_create_date <= ' . strtotime($year . '-12-31').' and status!='.ORDER_STATUS_NO_CANCEL,
						'order' => 'order_create_date ASC'
				));
				$quantity = array(0=>0,1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0,11=>0,12=>0);
				foreach ($orders as $k => $v) {
					$quantity[0] +=$v['quantity'];
					switch (intval(date('m', $v['order_create_date']))) {
						case 1 :
							$quantity[1] +=$v['quantity'];
							break;
						case 2 :
							$quantity[2] +=$v['quantity'];
							break;
						case 3 :
							$quantity[3] +=$v['quantity'];
							break;
						case 4 :
							$quantity[4] +=$v['quantity'];
							break;
						case 5 :
							$quantity[5] +=$v['quantity'];
							break;
						case 6 :
							$quantity[6] +=$v['quantity'];
							break;
						case 7 :
							$quantity[7] +=$v['quantity'];
							break;
						case 8 :
							$quantity[8] +=$v['quantity'];
							break;
						case 9 :
							$quantity[9] +=$v['quantity'];
							break;
						case 10 :
							$quantity[10] +=$v['quantity'];
							break;
						case 11 :
							$quantity[11] +=$v['quantity'];
							break;
						case 12 :
							$quantity[12] +=$v['quantity'];
							break;
					}
				}
				$user_list[$key]['quantitys'] = $quantity;
				$this->assign('user_list', $user_list);
			}
			$this->display('schedule.consumer_order.html');
			return;
		}else{
			$merch_list = $this->_member_mod->find(array(
					'conditions' => 'if_show = 1 and role='.USER_MERCHANDISER.$con,
					'order' => 'user_name ASC'
			));
			foreach ($merch_list as $key => $val) {
				$user_list = $this->_member_mod->find(array(
						'join'=>'has_consumer',
						'conditions' => 'if_show=1 and activation=1 and freight_id = ' . $val['user_id'] . ' AND reg_time >= ' . strtotime($this_year . '-1-1') . ' AND reg_time <= ' . strtotime($this_year . '-12-31').' and role='.USER_CLIENT,
				));
				$merch_list[$key]['user_count'] = count($user_list);
				$quantity = array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0,11=>0,12=>0);
				foreach ($user_list as $k => $v) {
					switch (intval(date('m', $v['reg_time']))) {
						case 1 :
							$quantity[1] ++;
							break;
						case 2 :
							$quantity[2] ++;
							break;
						case 3 :
							$quantity[3] ++;
							break;
						case 4 :
							$quantity[4] ++;
							break;
						case 5 :
							$quantity[5] ++;
							break;
						case 6 :
							$quantity[6] ++;
							break;
						case 7 :
							$quantity[7] ++;
							break;
						case 8 :
							$quantity[8] ++;
							break;
						case 9 :
							$quantity[9] ++;
							break;
						case 10 :
							$quantity[10] ++;
							break;
						case 11 :
							$quantity[11] ++;
							break;
						case 12 :
							$quantity[12] ++;
							break;
					}
				}
				$merch_list[$key]['quantitys'] = $quantity;
				$this->assign('merch_list', $merch_list);
			}
			$this->display('schedule.total.html');
		}
	}
	
	
	function consumer_list(){
		$user=$this->visitor->get();
		$conditions=$this->_get_query_conditions(array(
			array(
					'field' => 'user_name',
					'equal' => 'like',
					'name'	=> 'user_name'
			),
			array(
					'field' => 'email',
					'equal' => 'like',
					'name'	=> 'email'
			),
			array(
					'field' => 'phone_mob',
					'equal' => 'like',
					'name'	=> 'phone_mob'
			),
			array(
					'field' => 'is_qq',
					'equal' => 'like',
					'name'	=> 'im_qq'
			),
			array(
					'field' => 'member.company_id',
					'equal' => '=',
					'name'	=> 'company_id'
			)
		));
		$brand_ids=isset($_GET['brand_id'])?$_GET['brand_id']:0;
		$page = $this->_get_page();
		$year=$_GET['year'];
		$merch_id=$_GET['merch_id'];
		$month=$_GET['month'];
		$brand_mod=&m('brand');
		if (($year && $merch_id) && !$month){
			$conditions.=" and freight_id=".$merch_id.' and reg_time>='.strtotime($year.'-1-1').' and reg_time<='.strtotime($year.'-12-31');
		}elseif ($year && $merch_id && $month){
			$conditions.=" and freight_id=".$merch_id.' and reg_time>='.strtotime($year.'-'.$month.'-1').' and reg_time<='.strtotime($year.'-'.$month.'-31');
		}
		if ($user['role']==USER_MERCHANDISER){
			$conditions.=" and freight_id=".$user['user_id'].' and role='.USER_CLIENT;
		}elseif ($user['role']==USER_MERCHANDISER_ADMIN){
			$mod_cm=&m("company_manager");
			$mod_brand=&m("brand");
			$brand_id=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
			$brands=array_keys($brand_id);
			$user_mod =& m('member');
			$buyer_list=$user_mod->find(array(
					'conditions'=>'role='.USER_CLIENT.' and activation=1'
			));
			$flag=0;
			if (!empty($buyer_list)){
				foreach ($buyer_list as $j=>$row){
					$brand_arr_id=explode(',', $row['brand_arr_id']);
					$brand_arr=array_merge($brand_arr_id,$brands);
					$brand_arr_ids=array_count_values($brand_arr);
					if (!empty($brand_arr_ids)){
						foreach ($brand_arr_ids as $i=>$r){
							if ($brand_arr_ids[$i]>1){
								$flag=1;
								break;
							}else{
								$flag=0;
							}
						}
					}
					if ($flag){
						$freight_id[]=$row['freight_id'];
					}
				}
			}
			$conditions.=" and role=".USER_CLIENT." and activation=1 and if_show=1 and freight_id".db_create_in(array_unique($freight_id));
		}else{
			$conditions.=" and role=".USER_CLIENT;
		}
		
		if ($brand_ids){
			$conditions.=" and FIND_IN_SET({$brand_ids},brand_arr_id)";
		}
		$order = empty($_GET['order']) ? 'reg_time DESC' : str_replace('-', ' ', trim($_GET['order']));
		if ($order != 'reg_time DESC' || $order != 'reg_time ASC') {
			$order .= ', reg_time DESC';
		}
		$member_list = $this->_member_mod->find(array(
				'join'=>'has_consumer,belongs_to_company',
				'conditions'=>'1=1'.$conditions,
				'fields'=>'*,freight_id as merch_name',
				'order' => $order,
				'count' => true,
				'limit' => $page['limit'],
		));
		if (!empty($member_list)){
			foreach ($member_list as $k=>$v){
				$brand_name_arr=array();
				$merch_id=$v['freight_id'];
				$merch_info=$this->_member_mod->get($merch_id);
				$member_list[$k]['merch_name']=$merch_info['user_name'];
				$brand_arr_id=$v['brand_arr_id'];
				$brand_list=$brand_mod->find(array(
					'conditions'=>'brand_id'.db_create_in($brand_arr_id),
					'fields'=>'brand_name'	
				));
				if (!empty($brand_list)){
					foreach ($brand_list as $row){
						$brand_name_arr[]=$row['brand_name'];
					}
				}
				$member_list[$k]['brand_name']=implode(',', $brand_name_arr);
			}
		}
		
		//公司
		$company_mod=&m('company');
		$this->assign('company_list',$company_mod->find(array('conditions'=>'1=1','order'=>'add_time desc')));
		
		//品牌
		$brands=$brand_mod->find(array(
			'join'=>'belong_company',
			'conditions'=>'if_show=1',
			'order'=>'brand_id desc'	
		));
		$this->assign('brand_list',$brands);
		$page['item_count'] = $this->_member_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('member_list', $member_list);
		$this->assign('user_info',$user);
		$this->display('schedule.consumer_list.html');
		/* 当前位置 */
		$this->_curlocal(LANG::get('consumer_list'));
	}
	
	function child_user(){
		$user_arr=array();
		$mod_cm=&m("company_manager");
		$mod_brand=&m("brand");
		$brand_id=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
		$brands=array_keys($brand_id);
		$user_mod =& m('member');
		$buyer_list=$user_mod->find(array(
				'conditions'=>'role='.USER_CLIENT.' and activation=1'
		));
		$flag=0;
		if (!empty($buyer_list)){
			foreach ($buyer_list as $j=>$row){
				$brand_arr_id=explode(',', $row['brand_arr_id']);
				$brand_arr=array_merge($brand_arr_id,$brands);
				$brand_arr_ids=array_count_values($brand_arr);
				if (!empty($brand_arr_ids)){
					foreach ($brand_arr_ids as $i=>$r){
						if ($brand_arr_ids[$i]>1){
							$flag=1;
							break;
						}else{
							$flag=0;
						}
					}
				}
				if ($flag){
					$freight_id[]=$row['freight_id'];
				}
			}
		}
		if (!empty($freight_id)){
			$member_list=$user_mod->find(array(
					'conditions'=>'role='.USER_CLIENT.' and activation=1 and freight_id'.db_create_in($freight_id)
			));
			if (!empty($member_list)){
				foreach ($member_list as $val){
					$member_arr[]=$val['user_id'];
				}
			}
		}
		$user_arr=$member_arr;
		return $user_arr;
	}
}